﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>新增客户信息</title>

    <link href="../Frozenui1.3/css/frozen.css" rel="stylesheet" />
    

    <script src="../JS/layer/layer.m.js"></script>
    <script src="../JS/zepto.min.js"></script>
    <script src="../Frozenui1.3/js/frozen.js"></script>
    <script src="../JS/avalon.mobile.min.js"></script>
    <script src="../JS/MobileCom.js?v=1.1"></script>
    <script>
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            //隐藏右上角按钮
            WeixinJSBridge.call('hideOptionMenu');
        });

        //MobileCom.delCookie("szhlcode");
        //if (MobileCom.getCookie("szhlcode") == null) {
        //    MobileCom.setCookie("szhlcode", "aeac70cc-c359-45f2-90ac-78cb7abe5816", 7);
        //}

        var height = 0;
        var model = avalon.define({
            $id: "khmodel",
            ctData: { "KHLeiBie": "", "KHName": "", "KHXinBie": "先生", "KHPhone": "", "KHDW": "", "KHZhiWu": "", "KHAdress": "", "KHFXRS": "" },
            cpData: [],
            isbj: "",
            savedata: function () {
                if (model.ctData.KHLeiBie == "") {
                    $.tips({
                        content: "请选择客户类别",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                if (model.ctData.KHName == "") {
                    $.tips({
                        content: "请输入客户姓名",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                if (model.ctData.KHXinBie == "") {
                    $.tips({
                        content: "请选择客户性别",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                if (model.ctData.KHPhone == "") {
                    $.tips({
                        content: "请输入手机号",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                else {
                    var reg = /^(?:13\d|14\d|15\d|17\d|18\d)-?\d{5}(\d{3}|\*{3})$/;
                    if (!reg.test(model.ctData.KHPhone)) {
                        $.tips({
                            content: "手机号格式错误",
                            stayTime: 2000,
                            type: "warn"
                        })
                        return;
                    }
                }
                if (model.ctData.KHDW == "") {
                    $.tips({
                        content: "请输入所属单位",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                if (model.ctData.KHZhiWu == "") {
                    $.tips({
                        content: "请输入职务",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }
                if (model.ctData.KHAdress == "") {
                    $.tips({
                        content: "请输入联系地址",
                        stayTime: 2000,
                        type: "warn"
                    })
                    return;
                }

                var lg = $.loading({
                    content: '保存中...',
                })
                $.post("/API/WXAPI.ashx?Action=ADDKHGL&r=" + Math.random(), { "P1": JSON.stringify(model.ctData.$model) }, function (data) {
                    lg.hide();
                    var data = $.parseJSON(data);
                    if (data.ErrorMsg == "") {
                        $("button").hide();
                        var el = $.tips({
                            content: "保存成功！！",
                            stayTime: 2000,
                            type: "success"
                        })
                        el.on("tips:hide", function () {
                            //window.location.reload();

                            if (model.isbj == "") {
                                window.location.replace(location.href);
                            }
                            else {
                                //$("button").show();
                                window.location.replace("UI_KHGL_LIST.html?r=" + Math.random());
                            }
                        })
                    }
                    else {
                        $.tips({
                            content: "保存失败，原因：" + data.ErrorMsg,
                            stayTime: 2000,
                            type: "warn"
                        })
                    }
                })
            },
            repshow: function () {
                $(this).parent().siblings().removeClass("active");
                $(this).parent().addClass("active");
            },
            selperson: function () {
                $("#content").hide();
                $("#iframek").show();

                var cheight = height;
                var cwidth = document.documentElement.clientWidth;

                $("#iframek").css("height", cheight);
                $("#iframek").css("width", cwidth);

            }

        });

        function goback() {
            $("#content").show();
            $("#iframek").hide();
        }
        function goconfirm(json) {
            if (json != null) {
                $("#kh_fzry").removeClass("ui-txt-muted");
                $("#kh_fzry").html(json.vuser);
                model.ctData.KHFXRS = json.vusername;
            }
            else {
                $("#kh_fzry").addClass("ui-txt-muted");
                $("#kh_fzry").html('请选择客户负责人员');
                model.ctData.KHFXRS = '';
            }

            $("#content").show();
            $("#iframek").hide();
        }

        avalon.ready(function () {
            var strId = MobileCom.getQueryString("ID");
            $("#iframek").attr("src", "UI_PERSON.html?r=" + Math.random());
            height = document.documentElement.clientHeight;
            var lg = $.loading({
                content: '加载中...'
            })
            if (strId) {
                model.isbj = "Y";
                $("#save").text("保存");
                $("title").text("修改客户信息");

                var n = 0;
                $.post("/API/WXAPI.ashx?Action=GETCOLUMNLIST&r=" + Math.random(), { "P1": "8" }, function (data) {

                    var data = $.parseJSON(data);
                    if (data.ErrorMsg == "") {
                        $("select").eq(0).empty();
                        for (var i = 0; i < data.Result.length; i++) {
                            $("select").eq(0).append('<option value="' + data.Result[i].TypeNO + '">' + data.Result[i].TypeNO + '</option>');
                        }

                        $.getJSON('/API/WXAPI.ashx?Action=GETKHGLMODEL&r=' + Math.random(), { P1: strId }, function (resultData) {

                            if (resultData.ErrorMsg == "") {
                                model.ctData = resultData.Result;
                                if (resultData.Result.KHFXRS) {
                                    $("#iframek").attr("src", "UI_PERSON.html?r=" + Math.random() + "&Users=" + resultData.Result.KHFXRS);
                                    $.getJSON('/API/SZHLAPI.ashx?Action=GETUSERSBYUSERNAME&r=' + Math.random(), { P1: resultData.Result.KHFXRS }, function (data) {
                                        lg.hide();
                                        if (data.ErrorMsg == "") {
                                            var html = '';
                                            for (var i = 0; i < data.Result.length; i++) {
                                                if (html) {
                                                    html = html + ',' + data.Result[i].UserRealName;
                                                }
                                                else {
                                                    html = data.Result[i].UserRealName
                                                }
                                            }

                                            $("#kh_fzry").removeClass("ui-txt-muted");
                                            $("#kh_fzry").html(html);
                                        }
                                    })
                                }
                                else {
                                    lg.hide();
                                }
                            } else {
                                lg.hide();
                            }
                        })
                    }
                    else {
                        lg.hide();
                        $.tips({
                            content: "客户类别加载失败，原因：" + data.ErrorMsg,
                            stayTime: 2000,
                            type: "warn"
                        })
                    }
                })
            }
            else {
                var n = 0;
                $.post("/API/WXAPI.ashx?Action=GETCOLUMNLIST&r=" + Math.random(), { "P1": "8" }, function (data) {
                    n = n + 1;
                    if (n == 2) {
                        lg.hide();
                    }
                    var data = $.parseJSON(data);
                    if (data.ErrorMsg == "") {
                        $("select").eq(0).empty();
                        for (var i = 0; i < data.Result.length; i++) {
                            $("select").eq(0).append('<option value="' + data.Result[i].TypeNO + '">' + data.Result[i].TypeNO + '</option>');
                            if (i == 0) {
                                model.ctData.KHLeiBie = data.Result[i].TypeNO;
                            }
                        }
                    }
                    else {
                        $.tips({
                            content: "客户类别加载失败，原因：" + data.ErrorMsg,
                            stayTime: 2000,
                            type: "warn"
                        })
                    }
                })

                $.post("/API/SZHLAPI.ashx?Action=GetUserInfo&r=" + Math.random(), {}, function (data) {
                    n = n + 1;
                    if (n == 2) {
                        lg.hide();
                    }
                    var data = $.parseJSON(data);
                    if (data.ErrorMsg == "") {
                        model.ctData.KHFXRS = data.Result.User.UserName;
                        $("#kh_fzry").removeClass("ui-txt-muted");
                        $("#kh_fzry").html(data.Result.User.UserRealName);
                        $("#iframek").attr("src", "UI_PERSON.html?r=" + Math.random() + "&Users=" + data.Result.User.UserName);
                    }
                })
            }
        })

        function back() {
            window.location.replace("UI_KHGL_LIST.html?r=" + Math.random());
        }
    </script>
    <style type="text/css">
        .ui-select {
            border: none;
            border: 0;
            background-color: #fff;
            appearance: none;
            -moz-appearance: none; /* Firefox */
            -webkit-appearance: none; /* Safari 和 Chrome */
        }

        .ms-controller, [ms-controller] {
            display: none;
        }
    </style>
</head>
<body ms-controller="khmodel">
    <div id="content">
        <header ms-if="isbj" class="ui-header ui-header-stable ui-border-b">
            <i class="ui-icon-return" onclick="back()"></i>
            <h1>客户信息修改</h1>
        </header>
        <div class="ui-footer ui-footer-stable ui-btn-group ui-border-t">
            <button  class="ui-btn-lg" onclick="back()">
                取消
            </button>
            <button class="ui-btn-lg ui-btn-primary" ms-click="savedata()" id="save">
                新增客户
            </button>
        </div>
        <section class="ui-container">
            <div class="ui-form ui-border-t">
                <div class="ui-form-item ui-border-b">
                    <label>
                        客户类别
                    </label>
                    <div class="ui-select ui-border-b">
                        <select ms-duplex="ctData.KHLeiBie"></select>
                    </div>
                </div>
                <div class="ui-form-item ui-border-b">
                    <label>
                        客户姓名
                    </label>
                    <input type="text" placeholder="请输入客户姓名" ms-duplex="ctData.KHName" />
                </div>
                <div class="ui-form-item ui-border-b ">
                    <label>
                        客户性别
                    </label>
                    <div class="ui-select ui-border-b">
                        <select ms-duplex="ctData.KHXinBie">
                            <option value="女士">女士</option>
                            <option value="先生">先生</option>
                        </select>
                    </div>
                </div>
                <div class="ui-form-item ui-border-b">
                    <label>
                        手机号
                    </label>
                    <input type="tel" placeholder="请输入手机号" ms-duplex="ctData.KHPhone" />

                </div>
                <div class="ui-form-item ui-border-b">
                    <label>
                        所属单位
                    </label>
                    <input type="text" placeholder="请输入所属单位" ms-duplex="ctData.KHDW" />

                </div>
                <div class="ui-form-item ui-border-b">
                    <label>
                        职务
                    </label>
                    <input type="text" placeholder="请输入职务" ms-duplex="ctData.KHZhiWu" />

                </div>
                <div class="ui-form-item ui-border-b">
                    <label>
                        联系地址
                    </label>
                    <input type="text" placeholder="请输入联系地址" ms-duplex="ctData.KHAdress" />

                </div>
                <div class="ui-form-item ui-border-b " style="height:auto;min-height:44px">
                    <label>
                        负责人
                    </label>
                    <div class="ui-txt-muted ui-whitespace ui-form-item-link" style="padding-left: 95px;text-align:justify" id="kh_fzry" ms-click="selperson()">请选择客户负责人员</div>
                    <!--<input type="text" placeholder="请输入客户负责人员" id="kh_fzry" ms-click="selperson()" readonly="readonly" />-->

                </div>
            </div>
        </section>
    </div>
    <iframe id="iframek" src="UI_PERSON.html" style="display: none; margin-bottom: -7px;" frameborder="0"></iframe>

    
</body>

</html>

